package com.haoxi.gateway.cmpp30msg;

/**
 * Title:CMPP3.0,2004.09
 */

import java.io.IOException;
import java.io.OutputStream;


/**
 * ʵ��CMPP3.0 CMPP_SUBMIT��
 */
public class CMPPSubmit {
    private byte outbytes[];

    /**
     * ����һ��CMPP_SUBMIT��
     *
     * @param SequenceID          ��Ϣ���к�
     * @param Msg_Id              ��Ϣ��ʶ
     * @param Pk_total            ��ͬMsg_Id����Ϣ������,��1��ʼ
     * @param Pk_number           ��ͬMsg_Id����Ϣ��ţ���1��ʼ
     * @param Registered_delivery �Ƿ�Ҫ�󷵻�״̬����
     * @param Msg_level           ��Ϣ����
     * @param Service_id          ҵ������
     * @param Fee_UserType        �Ʒ��û������ֶ�
     * @param Fee_terminal_id     ���Ʒ��û��ĺ���
     * @param TP_pid              GSMЭ������
     * @param TP_udhi             GSMЭ������
     * @param Msg_fmt             ��Ϣ��ʽ
     * @param Msg_src             ��Ϣ����4Դ
     * @param FeeType             �ʷ�����
     * @param FeeCode             �ʷѴ���
     * @param Valid_time          �����Ч��
     * @param At_time             ��ʱ����ʱ��
     * @param Src_terminal_id     Դ����
     * @param user_number         ������Ϣ���û���
     * @param phone_num           ���ն��ŵ�MSISDN����
     * @param Msg_Content         ��Ϣ����
     * @throws CMPP30Exception ���ֶβ����CMPP3.0Э��Ҫ��ʱ
     */
    public CMPPSubmit ( int SequenceID, byte[] Msg_Id, int Pk_total,
                        int Pk_number, int Registered_Delivery,
                        int Msg_level, String Service_id, int Fee_UserType,
                        String Fee_terminal_id, int Fee_terminal_type, int TP_pid, int TP_udhi,
                        int Msg_fmt, String Msg_src, String FeeType,
                        String FeeCode, String Valid_time, String At_time,
                        String Src_terminal_id, int user_number,
                        String phone_num[], int Dest_terminal_type, byte[] Msg_Content, String LinkID )
            throws CMPP30Exception {
        if ( Src_terminal_id == null ) {
            throw new CMPP30Exception ( "Src_terminal_id is NULL" );
        }
        if ( phone_num == null ) {
            throw new CMPP30Exception ( "Dest_terminal_id is NULL" );
        }
        if ( Msg_Id.length != 8 ) //Msg_Idһ��Ҫ8λ
        {
            throw new CMPP30Exception ( "Msg_Id is invalid!" );
        }
        if ( Service_id == null || Service_id.length () == 0 )//ҵ�����һ��Ҫ��
        {
            throw new CMPP30Exception ( "Service_id is invalid!" );
        }
        if ( Msg_src == null || Msg_src.length () == 0 )//��Ϣ����4Դ��SP_id��һ��Ҫ��
        {
            throw new CMPP30Exception ( "Msg_src is invalid!" );
        }
        if ( FeeType == null || FeeType.length () != 2 )//�ʷ����һ��Ҫ����Ƚ���}λ
        {
            throw new CMPP30Exception ( "FeeType is invalid!" );
        }
        if ( FeeCode == null || FeeCode.length () != 6 )//�ʷѴ��루�Է�Ϊ��λ��һ��Ҫ����Ƚ�����λ
        {
            throw new CMPP30Exception ( "FeeCode is invalid!" );
        }
        if ( ( Registered_Delivery == 0 && user_number >= 100 ) || ( Registered_Delivery != 0 && user_number != 1 ) )//Ŀ���ֻ�������ֻ���ڲ���Ҫ״̬����ʱ�ſ���С��100������ֻ��1��
        {
            throw new CMPP30Exception ( "too much Dest_terminal!" );
        }
        if ( ( Msg_fmt == 0 && Msg_Content.length > 160 ) || ( Msg_fmt != 0 && Msg_Content.length > 140 ) )//��Ϣ����(Msg_FmtֵΪ0ʱ��<160���ֽ�;����<=140���ֽ�)
        {
            throw new CMPP30Exception ( "Msg_Content length is invalid!" );
        }
        int Msg_Length = Msg_Content.length;
        int total_length = CMPPCommon.MSG_HEAD_LENGTH + 151 + 32 * user_number + Msg_Length;
        CMPPMsgHead head = new CMPPMsgHead ();
        head.setCommandID ( CMPPCommon.CMPP_SUBMIT );
        head.setSequenceID ( SequenceID );
        head.setCommandLength ( total_length );
        byte[] bodybytes = new byte[total_length - CMPPCommon.MSG_HEAD_LENGTH];
        CMPPCommon.BytesCopy ( Msg_Id, bodybytes, 0, 7, 0 );
        bodybytes[ 8 ] = ( byte ) Pk_total;
        //MainSystem.logPrint("SequenceID="+SequenceID);
        //MainSystem.logPrint("Pk_total="+Pk_total);
        bodybytes[ 9 ] = ( byte ) Pk_number;
        //MainSystem.logPrint("Pk_number="+Pk_number);
        bodybytes[ 10 ] = ( byte ) Registered_Delivery;
        //MainSystem.logPrint("Registered_Delivery="+Registered_Delivery);
        bodybytes[ 11 ] = ( byte ) Msg_level;
        //MainSystem.logPrint("Msg_level="+Msg_level);
        byte tempbytes[] = new byte[10];
        //System.arraycopy(Service_id.getBytes(),0,bodybytes,12,Service_id.length());
        Service_id.getBytes ( 0, Service_id.length (), tempbytes, 0 );
        //MainSystem.logPrint("Service_id="+Service_id);
        CMPPCommon.BytesCopy ( tempbytes, bodybytes, 0, 9, 12 );
        bodybytes[ 22 ] = ( byte ) Fee_UserType;
        //MainSystem.logPrint("Fee_UserType="+Fee_UserType);
        tempbytes = new byte[32];
        Fee_terminal_id.getBytes ( 0, Fee_terminal_id.length (), tempbytes, 0 );
        //MainSystem.logPrint("Fee_terminal_id="+Fee_terminal_id);
        CMPPCommon.BytesCopy ( tempbytes, bodybytes, 0, 31, 23 );
        bodybytes[ 55 ] = ( byte ) Fee_terminal_type;
        //MainSystem.logPrint("Fee_terminal_type="+Fee_terminal_type);
        bodybytes[ 56 ] = ( byte ) TP_pid;
        //MainSystem.logPrint("TP_pid="+TP_pid);
        bodybytes[ 57 ] = ( byte ) TP_udhi;
        //MainSystem.logPrint("TP_udhi="+TP_udhi);
        bodybytes[ 58 ] = ( byte ) Msg_fmt;
        //MainSystem.logPrint("Msg_fmt="+Msg_fmt);
        tempbytes = new byte[6];
        Msg_src.getBytes ( 0, Msg_src.length (), tempbytes, 0 );
        //MainSystem.logPrint("Msg_src="+Msg_src);
        CMPPCommon.BytesCopy ( tempbytes, bodybytes, 0, 5, 59 );
        FeeType.getBytes ( 0, 2, bodybytes, 65 );
        //MainSystem.logPrint("FeeType="+FeeType);
        FeeCode.getBytes ( 0, FeeCode.length (), bodybytes, 67 );
        //MainSystem.logPrint("FeeCode="+FeeCode);

        tempbytes = new byte[17];
        //java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyMMddHHmmss");
        At_time = "";
        At_time.getBytes ( 0, At_time.length (), tempbytes, 0 );
        //MainSystem.logPrint("At_time="+At_time);
        CMPPCommon.BytesCopy ( tempbytes, bodybytes, 0, 16, 90 );

        //if (Valid_time==null||Valid_time.equals(""))
        //{
        //java.util.Calendar cal=java.util.Calendar.getInstance();
        //cal.add(cal.DAY_OF_MONTH,2);
        //Valid_time= sdf.format(cal.getTime())+"032+";
        Valid_time = "";
        //}
        tempbytes = new byte[17];
        Valid_time.getBytes ( 0, Valid_time.length (), tempbytes, 0 );
        //MainSystem.logPrint("Valid_time="+Valid_time);
        CMPPCommon.BytesCopy ( tempbytes, bodybytes, 0, 16, 73 );

        tempbytes = new byte[21];
        Src_terminal_id.getBytes ( 0, Src_terminal_id.length (), tempbytes, 0 );
        //MainSystem.logPrint("Src_terminal_id="+Src_terminal_id);
        CMPPCommon.BytesCopy ( tempbytes, bodybytes, 0, 20, 107 );
        int cur_pos = 128;
        bodybytes[ cur_pos ] = ( byte ) user_number;
        cur_pos++;
        int i;
        for ( i = 0; i < user_number; i++ ) {
            tempbytes = new byte[32];
            String mobile = phone_num[ i ];
            mobile.getBytes ( 0, mobile.length (), tempbytes, 0 );
            CMPPCommon.BytesCopy ( tempbytes, bodybytes, 0, 31, cur_pos );
            cur_pos += 32;
        }
        bodybytes[ cur_pos ] = ( byte ) Dest_terminal_type;
        //MainSystem.logPrint("Dest_terminal_type="+Dest_terminal_type);
        cur_pos++;
        bodybytes[ cur_pos ] = ( byte ) Msg_Length;
        //MainSystem.logPrint("Msg_Length="+Msg_Length);
        cur_pos++;
        CMPPCommon.BytesCopy ( Msg_Content, bodybytes, 0, Msg_Length - 1, cur_pos );
        //MainSystem.logPrint("Msg_Content="+Msg_Content);

        tempbytes = new byte[20];
        if ( LinkID == null || LinkID.equals ( "" ) ) {
            LinkID = "0";
        }
        LinkID.getBytes ( 0, LinkID.length (), tempbytes, 0 );
        //MainSystem.logPrint("LinkID="+LinkID);
        cur_pos = cur_pos + Msg_Length;
        //MainSystem.logPrint("cur_pos="+cur_pos);
        CMPPCommon.BytesCopy ( tempbytes, bodybytes, 0, 19, cur_pos );//LinkID

        byte[] msgHead = head.getMsgHead ();
        outbytes = new byte[total_length];
        //MainSystem.logPrint("total_length="+total_length);
        CMPPCommon.BytesCopy ( msgHead, outbytes, 0, msgHead.length - 1, 0 );
        CMPPCommon.BytesCopy ( bodybytes, outbytes, 0, bodybytes.length - 1, msgHead.length );
        //MainSystem.logPrint("bodybytes.length:"+bodybytes.length);
    }

    /**
     * ����ֽ�������ʽ��CMPPSubmit��
     *
     * @param out ����ͨ��
     * @throws IOException �������Υ��ʱ
     */
    public void write ( OutputStream out ) throws IOException {
        out.write ( outbytes );
        out.flush ();
    }

    /**
     * ȡ���ֽ�������ʽ��CMPPSubmit��
     *
     * @return �ֽ�������ʽ��CMPPSubmit��
     */
    public byte[] getBytes () {
        return outbytes;
    }
}